<?php

namespace teach\controllers;

use teach\service\Cart;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

class CartController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 添加购物车
     * @return \yii\web\Response
     */
    public function actionAdd()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');
        $pid = $request->post('pid');
        $uid = $request->post('uid');
        $sku_id = $request->post('sku_id');
        $count = $request->post('count');

        $msg = '';
        $flag = false;
        if (!$mid) {
            $msg = 'mid 商家id不能为空';
        } elseif (!$pid) {
            $msg = 'pid 产品id不能为空';
        } elseif (!$uid) {
            $msg = 'uid 用户id不能为空';
        } elseif (!$count) {
            $msg = '数量不能为空';
        } elseif (!$sku_id) {
            $msg = 'sku_id 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $carts = new Cart();

            $param = " mid = {$mid} AND sku_id = {$sku_id} AND pid = {$pid} AND uid = {$uid} ";
            $having = $carts->cartHaving($param);

            if ($having > 0) {
                //存在则加入到里面
                $res = $carts->cartAddUpdate($param, $count);
                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '添加成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '添加失败,请联系商家');
                }
            } else {
                $map = [
                    'mid' => $mid,
                    'sku_id' => $sku_id,
                    'pid' => $pid,
                    'uid' => $uid,
                    'is_unchange' => 1,
                    'create_time' => time(),
                    'update_time' => time(),
                    'count' => $count
                ];
                $res = $carts->cartAdd($map);
                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '添加成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '添加失败,请联系商家');
                }
            }

        }

        return $this->asJson($jsonData);
    }

    /**
     * 购物车列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        $request = Yii::$app->request;
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$uid) {
            $msg = 'uid 用户id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $carts = new Cart();
            $map = " uid = {$uid} ";
            $cartList = $carts->cartsList($map);

            $jsonData = ResponseHelper::buildJsonData(200, '成功', $cartList);
        }

        return $this->asJson($jsonData);
    }

    /**
     * 删除购物车
     * @return \yii\web\Response
     */
    public function actionDelete()
    {
        $request = Yii::$app->request;
        $cart_id = $request->post('cart_id');
        $uid = $request->post('uid');

        $msg = '';
        $flag = false;
        if (!$cart_id) {
            $msg = 'cart_id 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 用户id不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $carts = new Cart();
            $arr_id = json_decode($cart_id, true);
            foreach ($arr_id as $item) {
                $map = " uid = {$uid} AND id = {$item['id']} ";
                $carts->cartDelete($map);
            }
            $jsonData = ResponseHelper::buildJsonData(200, '成功');
        }

        return $this->asJson($jsonData);
    }

    /**
     * 修改购物车商品数量
     */
    public function actionChange()
    {
        $request = Yii::$app->request;
        $cid = $request->post('cid');
        $uid = $request->post('uid');
        $count = $request->post('count');

        $msg = '';
        $flag = false;

        if (!$cid) {
            $msg = 'cid 购物车id不能为空';
        } elseif (!$uid) {
            $msg = 'uid 用户id不能为空';
        } elseif (!$count) {
            $msg = 'count 数量不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $carts = new Cart();
            $map = " count = {$count} ";
            $mapper = " id = {$cid} AND uid = {$uid} ";
            $res = $carts->cartChange($map,$mapper);
            if ($res) {
                $jsonData = ResponseHelper::buildJsonData(200, '修改成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '修改失败');
            }

        }

        return $this->asJson($jsonData);
    }


}